<template>
  <div class="search-container">
    <!-- 搜索栏 -->
    <!--
      Tips: 在 van-search 外层增加 form 标签，且 action 不为空，即可在 iOS 输入法中显示搜索按钮
     -->
    <form action="/">
      <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        background="#3296fa"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>
    <!-- /搜索栏 -->

    <!-- 搜索结果 -->
    <div v-if="isResultShow" class="search_result_box">
      <SearchResult />
    </div>
    <!-- /搜索结果 -->

    <!-- 联想建议 -->
    <SearchSuggestion v-else-if="searchText" :searchText="searchText" />
    <!-- /联想建议 -->

    <!-- 搜索历史记录 -->
    <SearchHistory v-else />
    <!-- /搜索历史记录 -->
  </div>
</template>

<script>
import SearchHistory from './components/SearchHistory'
import SearchSuggestion from './components/SearchSuggestion'
import SearchResult from './components/SearchResult'

export default {
  name: 'SearchIndex',
  components: {
    SearchHistory,
    SearchSuggestion,
    SearchResult,
  },
  data() {
    return {
      searchText: '',
      isResultShow: false,
    }
  },
  methods: {
    onSearch(val) {
      this.isResultShow = true
    },
    onCancel() {
      this.$router.back()
    },
  },
}
</script>

<style scoped lang="less">
.search-container {
  .search_result_box {
    height: calc(100vh - 108px);
    overflow-y: auto;
  }

  .van-search__action {
    color: #fff;
  }
}
</style>
